//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package cc.bukkitPlugin.pds.task;

import cc.bukkitPlugin.pds.PlayerDataSQL;
import cc.bukkitPlugin.pds.commons.Log;
import cc.bukkitPlugin.pds.commons.util.ToolKit;
import cc.bukkitPlugin.pds.events.PlayerDataLoadCompleteEvent;
import cc.bukkitPlugin.pds.user.User;
import cc.bukkitPlugin.pds.user.UserManager;
import cc.bukkitPlugin.pds.util.CPlayer;
import java.util.concurrent.Executors;
import java.util.function.Consumer;

import ccy.ccyarmourersworkshopupdateplayerskin.CcyArmourersWorkshopUpdatePlayerSkin;
import ccy.ccylibrary.command.CommandExecution;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

public class LoadUserTask implements Runnable {
    private final int RETRY_COUNT;
    private final CPlayer mPlayer;
    private final String mName;
    private UserManager mUserMan;
    protected int mRetry;
    protected PlayerDataSQL mPlugin;
    protected boolean mDone;
    private int mInterval;

    public LoadUserTask(Player pForPlayer, UserManager pUserMan) {
        this(new CPlayer(pForPlayer), pUserMan);
    }

    public LoadUserTask(String pForPlayer, UserManager pUserMan) {
        this(new CPlayer(pForPlayer), pUserMan);
    }

    public LoadUserTask(CPlayer pForPlayer, UserManager pUserMan) {
        this.RETRY_COUNT = 5;
        this.mDone = false;
        this.mPlayer = pForPlayer;
        this.mName = pForPlayer.getName();
        this.mUserMan = pUserMan;
        this.mPlugin = (PlayerDataSQL)pUserMan.getPlugin();
        this.mInterval = ToolKit.between(1, 200, this.mPlugin.getConfigManager().mSyncDelay) * 50;
    }

    public void run() {
        if (this.mPlayer.isValid()) {
            while(this.mUserMan.isLocked(this.mPlayer.getPlayer())) {
                try {
                    Thread.sleep((long)this.mInterval);
                } catch (InterruptedException var3) {
                    Log.severe(this.mPlugin.C("MsgErrorOnLoadingDataWait"), var3);
                }

                if (this.mDone || this.mRetry > 5 || !this.mPlugin.isEnabled()) {
                    return;
                }

                ++this.mRetry;
                User tUser = null;

                try {
                    tUser = this.mUserMan.loadUser(this.mPlayer);
                } catch (Throwable var4) {
                    Log.severe("载入玩家 " + this.mName + " 的数据时发生错误", var4);
                    if (!this.tryKickIfError()) {
                        continue;
                    }

                    return;
                }

                try {
                    if ((tUser == null || tUser.isLocked()) && this.mRetry <= 5) {
                        Log.debug("Load user data " + this.mName + " fail " + this.mRetry + (tUser == null ? "(no data and wait)" : "(Locked)"));
                    } else {
                        if (tUser != null && tUser.isLocked()) {
                            if (!this.mPlugin.getConfigManager().mForceUseLockData && this.tryKickIfError()) {
                                Log.info("用户数据锁定,踢出玩家");
                                return;
                            }

                            Log.warn("Use locked data to restore user " + this.mName);
                        }
                        this.restoreUser(tUser, false);
                        Bukkit.getScheduler().callSyncMethod(this.mPlugin, Executors.callable(() -> {
                            Bukkit.getPluginManager().callEvent(new PlayerDataLoadCompleteEvent(this.mPlayer.getPlayer()));
                        }));

                        CommandExecution.Command(this.mPlayer.getPlayer(),"cmd:sudo <player> skinupdate");
                        break;
                    }
                } catch (Throwable var5) {
                    Log.severe(var5);
                }
            }

            if (!this.mDone && !this.mUserMan.isLocked(this.mPlayer.getPlayer())) {
                Log.debug("Cancel load data task for " + this.mName + ", player may already quit game");
            }

        }
    }

    protected boolean tryKickIfError() {
        if (this.mPlugin.getConfigManager().mKickOnReadSQLError) {
            PlayerDataSQL.kickPlayerOnError(this.mPlayer);
            this.mDone = true;
            return true;
        } else {
            return false;
        }
    }

    protected void restoreUser(User pUser, boolean pException) {
        this.mDone = true;
        boolean tNoRestore = this.mPlugin.getConfigManager().mNoRestoreIfSQLDataNotExist;
        if (pUser == null && (pException || !tNoRestore)) {
            Log.debug("Use blank data restore for player " + this.mName);
            pUser = new User(this.mPlayer);
        }

        Consumer<Boolean> tCall = (pSuccess) -> {
            if (pSuccess && this.mUserMan.isLocked(this.mPlayer.getPlayer())) {
                this.mUserMan.createSaveTask(this.mPlayer);
            }

            this.mUserMan.unlockUser(this.mPlayer.getPlayer(), false);
        };
        if (pUser != null) {
            this.mUserMan.restoreUser(this.mPlayer, pUser, tCall);
        } else {
            Log.debug("duce setting NoRestoreIfSQLDataNotExist=true,plugin skip load user data");
            tCall.accept(false);
        }

    }
}